package cn.xingzhen.jfield.helper;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;

/**
 * @author wenqiang
 * @date 2023/5/11
 * @comment ..
 */
public class StringHelper {

    public static String truncStr(Object... strs) {
        if (strs == null) return null;
        if (strs.length < 2) return merge(strs);
        String str0 = strs[0].toString();
        if (!str0.matches("(?s).*?\\{.*?\\}.*?")) {
            return merge(strs);
        }
        int len = strs.length;
        for (int i = 1; i < len; i++) {
            /*
            String s = regexFind(str0, "\\{\\}");
            if (s.startsWith("{")) {
                s = "\\" + s;
            }
            if (s.endsWith("}")) {
                s = s.substring(0, s.length() - 1);
                s += "\\}";
            }*/
            String s = "(?s)\\{\\}";
            if (strs[i] != null) {
                String v = String.valueOf(strs[i]);
                str0 = str0.replaceFirst(s, Matcher.quoteReplacement(v));
            }
        }
        return str0;
    }

    /**
     * 数组合并（toString()）
     */
    public static String merge(Object... strs) {
        StringBuilder sb = new StringBuilder();
        int len = strs.length;
        for (int i = 0; i < len; i++) {
            sb.append(strs[i].toString());
        }
        return sb.toString();
    }

    public static String humpString(String str) {
        if (str == null || str.length() == 0) return null;
        StringBuilder sb = new StringBuilder();
        char[] chars = str.toCharArray();
        for (int i = 0, size = chars.length; i < size; i++) {
            String b = String.valueOf(chars[i]);
            if(b.matches("[\\-_]")) {
                continue;
            }
            if(i == 0 || i == size-1) {
                sb.append(b.toLowerCase());
                continue;
            }
            String a = String.valueOf(chars[i-1]);
            if(a.matches("[A-Z]")) {
                sb.append(b.toLowerCase());
                continue;
            }
            if(a.matches("[a-z]") && b.matches("[A-Z]")) {
                sb.append(b);
                continue;
            }
            if(a.matches("[\\-_]")) {
                sb.append(b.toUpperCase());
                continue;
            }
            sb.append(b);
        }
        return sb.toString();
    }

    public static boolean isEmpty(Object object) {
        if (object == null) return true;
        if (object instanceof String && ((String) object).isEmpty()) return true;
        if (object instanceof Map<?, ?> && ((Map<?, ?>) object).isEmpty()) return true;
        return object instanceof List<?> && ((List<?>) object).isEmpty();
    }

    public static boolean isNotEmpty(Object object) {
        return !isEmpty(object);
    }
}
